package team.epos.merchant.servlet;

import team.epos.merchant.dto.ResponseBody;
import team.epos.merchant.service.StoreService;
import team.epos.merchant.service.StoreServiceImpl;
import team.epos.utils.annotation.RequestMethod;
import team.epos.utils.pojo.Advert;
import team.epos.utils.pojo.Apply;
import team.epos.utils.pojo.Store;
import team.epos.utils.servlet.BaseServlet;
import team.epos.utils.util.JsonUtil;
import team.epos.utils.util.PojoParamUtil;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * Create by Vic Xu on 7/14/2018
 *
 * @author Vic Xu
 */
public class StoreServlet extends BaseServlet {

    private static final long serialVersionUID = -7794713015386952291L;
    private StoreService storeService = new StoreServiceImpl();

    @RequestMethod(method = "openStore")
    public void openStore(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        Store store = PojoParamUtil.getObject(req, Store.class);
        int i = 0;
        i = storeService.openStore(store, req);
        if (i > 0) {
            resp.getWriter().write(JsonUtil.obJectToJson(new ResponseBody("success", i)));
        } else {
            resp.getWriter().write("请求数据格式有误，提交失败");
        }
    }

    @RequestMethod(method = "recommend")
    public void recommendStore(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String sId = req.getParameter("mId");
        req.setAttribute("mId", sId);
        req.getRequestDispatcher("/jsp/recommend.jsp").forward(req, resp);
    }

    @RequestMethod(method = "subRecommend")
    public void subRecommend(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        Advert advert = PojoParamUtil.getObject(req, Advert.class);
        storeService.subRecommend(advert, req);
        resp.getWriter().write(JsonUtil.obJectToJson(new ResponseBody("success")));
    }

    @RequestMethod(method = "getStoreData")
    public void getStoreData(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        String sId = req.getParameter("sId");
        Store store = storeService.rejectApply(Integer.parseInt(sId));
        resp.getWriter().write(JsonUtil.obJectToJson(store));
    }


}
